using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using FAQComponents;
using FAQComponents.API;
using FAQComponents.Objects;
using Adge.UI.WebControls;

namespace Adge.Web.Faq.Admin
{
	/// <summary>
	/// Summary description for RelatedFAQs.
	/// </summary>
	public partial class RelatedFAQs : ClientSidePage
	{
	
		private int FAQID;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			FAQID = InvariantString.ToInt32(Request.QueryString["ID"]);

			base.BypassModifiedMethod(btnUpdate);

			if (!Page.IsPostBack)
			{
				FAQ f = FAQAPI.GetFAQ(FAQID);
				ltlFAQName.Text = f.Question;

				BindData();
			}
		}

		private FAQCollection relatedFAQs = null;
		private void BindData()
		{
			relatedFAQs = FAQAPI.GetRelatedFAQs(FAQID);

			dlCategories.DataSource = CategoryAPI.GetAllCategories();
			dlCategories.DataBind();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.dlCategories.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.dlCategories_ItemDataBound);

		}
		#endregion

		private void dlCategories_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				CheckBoxList cbl = (CheckBoxList) e.Item.FindControl("chkFAQs");
				cbl.DataSource = FAQAPI.GetFAQsInCategory((int) DataBinder.Eval(e.Item.DataItem, "CategoryID"), true, false);
				cbl.DataBind();

				// Check the appropriate checkboxes
				foreach(ListItem li in cbl.Items)
					li.Selected = relatedFAQs.ContainsFAQID(InvariantString.ToInt32(li.Value));

				cbl.Items.Remove(cbl.Items.FindByValue(FAQID.ToString()));

				base.MonitorChanges(cbl);
			}
		}

		protected void btnCancel_Click(object sender, System.EventArgs e)
		{
			// send the user back to the FAQ admin or FAQ maintainence screen
			if (Request.QueryString["from"] != null)
				Response.Redirect(Request.QueryString["from"].ToString());
			else
				Response.Redirect("FAQAdmin.aspx");
		}

		protected void btnUpdate_Click(object sender, System.EventArgs e)
		{
			// Save the data...
			foreach(DataListItem dli in dlCategories.Items)
			{
				CheckBoxList cbl = (CheckBoxList) dli.FindControl("chkFAQs");
				foreach (ListItem li in cbl.Items)
					if (li.Selected)
						FAQAPI.AddRelatedFAQ(FAQID, InvariantString.ToInt32(li.Value));
					else
						FAQAPI.DeleteRelatedFAQ(FAQID, InvariantString.ToInt32(li.Value));
			}
		
			// send the user back to the FAQ admin or FAQ maintainence screen
			if (Request.QueryString["from"] != null)
				Response.Redirect(Request.QueryString["from"].ToString());
			else
				Response.Redirect("FAQAdmin.aspx");
		}
	}
}
